package cn.com.dragonsoftbravo.utils.azure.message;

import java.util.Date;
import java.util.UUID;

import org.apache.log4j.Logger;

import cn.com.dragonsoftbravo.utils.DateUtils;
import cn.com.dragonsoftbravo.utils.SystemConfigHolder;
import cn.com.dragonsoftbravo.utils.azure.AzureConstants;
import cn.com.dragonsoftbravo.utils.azure.AzureServiceBusMessage;
import cn.com.dragonsoftbravo.utils.azure.factory.ServiceBusContractFactory;

import com.microsoft.windowsazure.exception.ServiceException;
import com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage;

public class ServiceBusQueueMessage implements AzureServiceBusMessage {

	private BrokeredMessage message;

	private Logger log = Logger.getLogger(ServiceBusQueueMessage.class);

	public ServiceBusQueueMessage() {
		message = new BrokeredMessage("");
		message.setMessageId(DateUtils.formatCompleteDate(new Date())
				+ UUID.randomUUID().toString().replace("-", "").toUpperCase().substring(0, 6));
	}

	public void setProperty(String key, String value) {
		message.setProperty(key, value);
	}

	public void sendOut() {
		try {
			String queuePath = SystemConfigHolder.instance().getValue(AzureConstants.QUEUE_PATH);
			ServiceBusContractFactory.getSerivceBusContract().sendQueueMessage(queuePath, message);
		} catch (ServiceException e) {
			log.error(e.getMessage());
		}
	}

}
